package com.liuceng.mps.service;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.listener.ConnectListener;
import com.corundumstudio.socketio.listener.DataListener;
import com.corundumstudio.socketio.listener.DisconnectListener;

@Service("socketIoService")
public class SocketIoService {
    static SocketIOServer server;
    static Map<String, SocketIOClient> clientsMapByAdminId = new HashMap<String, SocketIOClient>();

    public void startServer() throws InterruptedException{
        Configuration config = new Configuration();
        config.setHostname("localhost");
        config.setPort(3000);
        server = new SocketIOServer(config);

        /**
         * 用户绑定
         */
        server.addEventListener("user_binding", String.class, new DataListener<String>(){
            @Override
            public void onData(SocketIOClient client, String data, AckRequest ackRequest) throws ClassNotFoundException {
                clientsMapByAdminId.put(data,client);
                System.out.println("客户端已连接：网销ID为" + data);
            }
        });

        /**
         * 链接
         */
        server.addConnectListener(new ConnectListener() {
            @Override
            public void onConnect(SocketIOClient client) {
                client.sendEvent("binding","user_binding");
            }
        });

        /**
         * 断开
         */
        server.addDisconnectListener(new DisconnectListener(){
            @Override
            public void onDisconnect(SocketIOClient client) {
                String sa = client.getRemoteAddress().toString();
                String clientIp = sa.substring(1,sa.indexOf(":"));//获取设备ip
                System.out.println(clientIp+"-------------------------"+"客户端已断开连接");
            }
        });
        server.start();
    }
    public void stopServer(){
        if(server != null){
            server.stop();
            server = null;
        }
    }
    /**
     *  给所有连接客户端推送消息
     * @param eventType 推送的事件类型
     * @param message  推送的内容
     */
    public void remindAll(String adminIds, String eventType,String message){
        if (adminIds.length() > 0) {
            adminIds = adminIds.substring(0,adminIds.length()-1);
        } else {
            return;
        }
        String[] adminId = adminIds.split(",");
        for(String admin: adminId){
            SocketIOClient client = (SocketIOClient)clientsMapByAdminId.get(admin);
            if(client != null){
                client.sendEvent(eventType,message);
            }
        }
    }
    /**
     * 给具体的客户端推送消息
     * @param data 推送的消息内容
     */
    public void assignCustomer(String adminId,String eventType,Map<String, String> data){
        try {
            if(adminId != null && !"".equals(adminId)){
                SocketIOClient client = (SocketIOClient)clientsMapByAdminId.get(adminId);
                if(client != null){
                    client.sendEvent(eventType,data);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static SocketIOServer getServer() {
        return server;
    }
}